package com.ley.yikang.controller;

import com.ley.yikang.Annotation.PassToken;
import com.ley.yikang.Annotation.UserLoginToken;
import com.ley.yikang.entity.Action;
import com.ley.yikang.entity.Admin;
import com.ley.yikang.entity.CheckTask;
import com.ley.yikang.service.AdminService;
import com.ley.yikang.util.ResultData;
import com.ley.yikang.util.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/admin")
public class AdminController {
    private final AdminService adminService;

    @Autowired
    public AdminController(AdminService adminService) {
        this.adminService = adminService;
    }

    @PassToken
    @PostMapping("/login")
    public ResultData<String> login(@RequestBody Admin admin) {
        Admin admin1 = adminService.findAdmin(admin);
        if (admin1 != null) {
            System.out.println("管理员登录成功！找到管理员用户！");
            // 返回token
            return ResultData.success(TokenUtil.getAdminToken(admin1), "登录成功！");
        } else {
            System.out.println("管理员登录失败！未找到管理员用户！");
            return ResultData.fail("登录失败！用户名或密码错误！");
        }
    }

    @UserLoginToken
    @GetMapping("/getAllCheckTask")
    public ResultData<List<CheckTask>> getAllCheckTask() {
        System.out.println("管理员查询所有检查任务！");
        return ResultData.success(adminService.getAllCheckTask(), "查询成功！");
    }

    @UserLoginToken
    @GetMapping("/getAllAction")
    public ResultData<List<Action>> getAllAction() {
        System.out.println("管理员查询所有活动！");
        return ResultData.success(adminService.getAllAction(), "查询成功！");
    }
}
